package com.zx.tradeclient.ctrl.index1;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.URLHelper;
import com.zx.base.widget.CommentDialog;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public class DetailActivity extends Activity {
	private TextView title, note;

	private WebView content;
	private ImageView line;
	private ImageButton news_detail_bottom_share, news_detail_bottom_com, news_detail_bottom_fav;
	public Button header_left, header_right;
	public TextView header_title;
	private CommentDialog comment;
	private String newsid;
	private TradeApp app;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (TradeApp) getApplication();
		Intent intent = getIntent();
		newsid = intent.getStringExtra("newsId");
		setContentView(R.layout.news_detail);
		header_left = (Button) findViewById(R.id.header_left);
		header_right = (Button) findViewById(R.id.header_right);
		header_title = (TextView) findViewById(R.id.header_title);
		header_left.setVisibility(View.VISIBLE);
		header_right.setVisibility(View.VISIBLE);
		header_left.setText("返回");
		header_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 结束
				DetailActivity.this.finish();
			}
		});
		header_right.setText("评论详情");
		header_right.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 跳到评论页面
				Intent intent = new Intent(DetailActivity.this, NewsCommentActivity.class);
				intent.putExtra("id", newsid);
				startActivity(intent);
			}
		});

		news_detail_bottom_share = (ImageButton) findViewById(R.id.news_detail_bottom_share);
		news_detail_bottom_share.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 分享内容
				Intent intent = new Intent(Intent.ACTION_SEND);

				intent.setType("text/plain");
				intent.putExtra(Intent.EXTRA_TEXT, title.getText());

				String fx = title.getText() + " " + URLHelper.mPrefix_main + "/newscontent_" + newsid + "_1.html" + " \n好玩游戏尽在掌兴游戏市场 www.9qmy.com";
				intent.putExtra(Intent.EXTRA_TEXT, fx);
				startActivity(Intent.createChooser(intent, getTitle()));

			}
		});
		news_detail_bottom_fav = (ImageButton) findViewById(R.id.news_detail_bottom_fav);
		news_detail_bottom_fav.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (app.user.getId() == null || app.user.getId().equals("")) {
					Toast toast = Toast.makeText(DetailActivity.this, "您尚未登录。", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else {
					favsaddTask();
				}

			}
		});

		news_detail_bottom_com = (ImageButton) findViewById(R.id.news_detail_bottom_com);
		news_detail_bottom_com.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 打开评论窗口
				// if (!app.user.getId().equals("")) {
				if (app.user.getId() == null || app.user.getId().equals("")) {
					Toast toast = Toast.makeText(DetailActivity.this, "您尚未登录。", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else {
					comment = new CommentDialog(DetailActivity.this, "评论", "提交评论", "取消", false);
					comment.show();
					comment.bt_yes.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// 提交评论
							String poll_con = comment.editor.getText().toString().trim();
							commentTask(poll_con);
							comment.dlg.dismiss();
						}
					});
					comment.bt_no.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// 取消
							comment.dlg.dismiss();
						}
					});
				}

				// } else {
				// Toast toastError = Toast.makeText(DetailActivity.this,
				// "您尚未登录无法评论资讯内容", Toast.LENGTH_SHORT);
				// toastError.setGravity(Gravity.CENTER, 0, 0);
				// toastError.show();
				// }
			}
		});

		title = (TextView) findViewById(R.id.news_detail_title);
		note = (TextView) findViewById(R.id.news_detail_note);
		content = (WebView) findViewById(R.id.news_detail_content);
		line = (ImageView) findViewById(R.id.news_detail_line);

		// progressDialog = WaitDialog.createDialog(DetailActivity.this);
		// progressDialog.setMessage("正在读取资讯内容...");
		// progressDialog.show();
		newsListTask(newsid);
	}

	private void newsListTask(String newsId) {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "newContent");
		map.put("id", newsId);
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {
					String sucess = response.getString("succeed");
					if (sucess.equals("000")) {
						title.setText(response.getString("title"));
						note.setText(response.getString("ptime") + "     编辑:" + response.getString("author"));
						line.setVisibility(View.VISIBLE);
						content.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
						content.loadUrl(response.getString("content"));
					} else {
						title.setText("如果您看到了这列信息请联系系统管理员");
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void commentTask(String content) {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "comments");
		map.put("userId", app.user.getId());
		map.put("targetId", newsid);
		map.put("score", "0");
		map.put("targetType", "1");
		map.put("content", content);
		map.put("phonemac", app.getPhoneType());
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {

				try {
					String sucess = response.getString("succeed");
					if (sucess.equals("000")) {
						Toast toastError = Toast.makeText(DetailActivity.this, "评论提交成功", Toast.LENGTH_SHORT);
						toastError.setGravity(Gravity.CENTER, 0, 0);
						toastError.show();

					} else {
						Toast toastError = Toast.makeText(DetailActivity.this, "评论提交失败", Toast.LENGTH_SHORT);
						toastError.setGravity(Gravity.CENTER, 0, 0);
						toastError.show();
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				// TODO Auto-generated method stub

			}
		});

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			DetailActivity.this.finish();

			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void favsaddTask() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "favourites");
		map.put("userId", app.user.getId());
		map.put("type", "1");
		map.put("infoId", newsid);
		map.put("infoName", header_title.getText().toString().trim());
		if (Constant.debug)
			Log.d("TAG", app.user.getId());

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {

					String str = response.getString("succeed");
					if (str.equals("000")) {
						Toast toast = Toast.makeText(DetailActivity.this, "收藏成功。", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					} else if (str.equals("002")) {
						Toast toast = Toast.makeText(DetailActivity.this, "已经收藏过了。", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					} else if (str.equals("001")) {
						Toast toast = Toast.makeText(DetailActivity.this, "错误。", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(DetailActivity.this, "网络超时。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		});
	}
}
